<template>
    <div class="index-lists">
        <el-card class="!border-none" shadow="never">
            <div class="mb-4">
                <el-button type="primary" @click="getLists">刷新列表</el-button>
            </div>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <el-button v-perms="['sections:add']" type="primary" @click="handleAdd()">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增
                </el-button>

            </div>
            <el-table
                class="mt-4"
                size="large"
                v-loading="loading"
                :data="lists"
            >
                <el-table-column label="ID" prop="id" min-width="80" />
                <el-table-column label="名称" prop="name" min-width="150" />
                <el-table-column label="状态" prop="isActive" min-width="80">
                    <template #default="{ row }">
                        <el-tag :type="row.isActive === '1' ? 'success' : 'danger'">
                            {{ row.isActive === '1' ? '启用' : '禁用' }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="菜单ID" prop="menuId" min-width="80" />
                <el-table-column label="创建时间" prop="createTime" min-width="150">
                    <template #default="{ row }">
                        {{ new Date(row.createTime * 1000).toLocaleString() }}
                    </template>
                </el-table-column>
                <el-table-column label="修改时间" prop="updateTime" min-width="150">
                    <template #default="{ row }">
                        {{ new Date(row.updateTime * 1000).toLocaleString() }}
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['sections:edit']"
                            type="primary"
                            link
                            @click="handleEdit(row)"
                        >
                            编辑
                        </el-button>
                        <el-button
                            v-perms="['sections:del']"
                            type="danger"
                            link
                            @click="handleDelete(row.id)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

        </el-card>
        <edit-popup
            v-if="showEdit"
            ref="editRef"
            @success="getLists"
            @close="showEdit = false"
        />
    </div>
</template>
<script lang="ts" setup name="sections">
import { ref, shallowRef, nextTick } from 'vue'
import { sectionsDelete, sectionsGetAllSub } from '@/api/sections'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const loading = ref(false)
const lists = ref<any[]>([])

// 获取列表数据
const getLists = async () => {
    try {
        loading.value = true
        const res = await sectionsGetAllSub({ })
        // API 直接返回数组
        lists.value = Array.isArray(res) ? res : []
    } catch (error) {
        console.error('获取列表失败:', error)
        lists.value = []
    } finally {
        loading.value = false
    }
}



const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (id: number) => {
    try {
        await feedback.confirm('确定要删除？')
        await sectionsDelete({ id })
        feedback.msgSuccess('删除成功')
        getLists()
    } catch (error) {
        console.error('删除失败:', error)
        feedback.msgError('删除失败')
    }
}

// 页面初始化时获取数据
getLists()
</script>
